package com.zhangmeng.tools.controller;

import com.zhangmeng.tools.utils.AlertUtils;
import javafx.application.Platform;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author : 芊芊墨客
 * @version : 1.0
 * @date : 2023-04-27 10:24
 */
@Slf4j
public class BatchUpdateFileNameController {

    @FXML
    public TextField dir;

    @FXML
    public TextArea result_show;

    @FXML
    public Button replace_button;

    @FXML
    public TextField replace_str;

    @FXML
    public TextField replace_after;

    @FXML
    public void initialize() {

        dir.textProperty().addListener((observable, oldValue, newValue) -> {
            if (newValue != null) {
                getFileInfos(newValue);
            }
        });

        replace_button.setOnAction(event -> {
            if (dir.getText().length() == 0) {
                AlertUtils.alert_warning("请选择替换的文件夹");
                return;
            }
            if (replace_str.getText().length() == 0) {
                AlertUtils.alert_warning("请输入替换的文字");
                return;
            }

            if (replace_after.getText().length() == 0) {
                replace_after.setText("");
            }
            getFileInfos(dir.getText(), replace_str.getText(), replace_after.getText());
        });
    }

    public void getFileInfos(String path_name) {
        Path path = Paths.get(path_name);
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(System.lineSeparator());
        try {
            Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    stringBuilder.append(dir);
                    stringBuilder.append(System.lineSeparator());
                    dirCount.incrementAndGet();
                    return super.preVisitDirectory(dir, attrs);
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    stringBuilder.append(file);
                    stringBuilder.append(System.lineSeparator());
                    fileCount.incrementAndGet();
                    return super.visitFile(file, attrs);
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
        stringBuilder.append("文件夹数:").append(dirCount);
        stringBuilder.append(System.lineSeparator());
        stringBuilder.append("文件数:").append(fileCount);
        stringBuilder.append(System.lineSeparator());
        result_show.setText(stringBuilder.toString());
    }

    public void getFileInfos(String path_name, String replace, String replace_after) {
        Path path = Paths.get(path_name);
        AtomicInteger dirCount = new AtomicInteger();
        AtomicInteger fileCount = new AtomicInteger();
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append(System.lineSeparator());

        new Thread(()->{
            try {
                Files.walkFileTree(path, new SimpleFileVisitor<Path>() {
                    @Override
                    public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                        dir.toString().replace(replace, replace_after);
                        stringBuilder.append(dir);
                        stringBuilder.append(System.lineSeparator());
                        dirCount.incrementAndGet();
                        return super.preVisitDirectory(dir, attrs);
                    }

                    @Override
                    public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                        String name = file.toString().replace(replace,replace_after);
                        copy_del_file(file,Paths.get(name));
                        stringBuilder.append(name);
                        stringBuilder.append(System.lineSeparator());
                        fileCount.incrementAndGet();
                        return super.visitFile(file, attrs);
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }

            stringBuilder.append("文件夹数:").append(dirCount);
            stringBuilder.append(System.lineSeparator());
            stringBuilder.append("文件数:").append(fileCount);
            stringBuilder.append(System.lineSeparator());
            Platform.runLater(()->{
                result_show.setText(stringBuilder.toString());
            });
        }).start();
    }


    public void copy_del_file(Path source,Path target){
        try {
            if (!Files.exists(target)){
                Files.createFile(target);
            }
            Files.copy(source, target,StandardCopyOption.REPLACE_EXISTING);//如果文件存在则覆盖
            Files.delete(source);//拷贝完删除源文件
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
